/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.graphics.g2d.ParticleEmitter.RangedNumericValue;

public class ParticleEmitterTest extends InputAdapter implements ApplicationListener {
	private SpriteBatch spriteBatch;
	ParticleEffect effect;
	int emitterIndex;
	Array<ParticleEmitter> emitters;
	int particleCount = 10;
	float fpsCounter;
	InputProcessor inputProcessor;

	private int fastrandomseed    = (int)Math.random() * Integer.MAX_VALUE;
        private int FASTRANDOMTOFLOAT = 1/Integer.MAX_VALUE;
        
	public void create () {
		spriteBatch = new SpriteBatch();

		effect = new ParticleEffect();
		effect.load(Gdx.files.internal("test4.p"), Gdx.files.internal(""));
		//effect.setPosition(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);
                
		// Of course, a ParticleEffect is normally just used, without messing around with its emitters.
                
		emitters = new Array(effect.getEmitters());
		effect.getEmitters().clear();
                ParticleEmitter em = emitters.get(1);
                
                em.setContinuous(false);
		effect.getEmitters().add(em);
                em = emitters.get(2);
                em.setContinuous(false);
                effect.getEmitters().add(em);
                //em = emitters.get(2);
                //em.setContinuous(false);
                //effect.getEmitters().add(em);
                effect.start();
                
		inputProcessor = new InputProcessor() {
			public boolean touchUp (int x, int y, int pointer, int button) {
				return false;
			}

			public boolean touchDragged (int x, int y, int pointer) {
				effect.setPosition(x, Gdx.graphics.getHeight() - y);
				return false;
			}

			public boolean touchDown (int x, int y, int pointer, int newParam) {
				// effect.setPosition(x, Gdx.graphics.getHeight() - y);
				ParticleEmitter emitter = emitters.get(emitterIndex);
				particleCount += 100;
				System.out.println(particleCount);
				particleCount = Math.max(0, particleCount);
				if (particleCount > emitter.getMaxParticleCount()) emitter.setMaxParticleCount(particleCount * 2);
				emitter.getEmission().setHigh(particleCount / emitter.getLife().getHighMax() * 1000);
				effect.getEmitters().clear();
				effect.getEmitters().add(emitter);
				return false;
			}

			public boolean keyUp (int keycode) {
				return false;
			}

			public boolean keyTyped (char character) {
				return false;
			}

			public boolean keyDown (int keycode) {
				ParticleEmitter emitter = emitters.get(emitterIndex);
				if (keycode == Input.Keys.DPAD_UP)
					particleCount += 5;
				else if (keycode == Input.Keys.DPAD_DOWN)
					particleCount -= 5;
				else if (keycode == Input.Keys.SPACE) {
					emitterIndex = (emitterIndex + 1) % emitters.size;
					emitter = emitters.get(emitterIndex);

					// if we've previously stopped the emitter reset it
					if (emitter.isComplete()) emitter.reset();
					particleCount = (int)(emitter.getEmission().getHighMax() * emitter.getLife().getHighMax() / 1000f);
				} else if (keycode == Input.Keys.ENTER) {
					emitter = emitters.get(emitterIndex);
					if (emitter.isComplete())
						emitter.reset();
					else
						emitter.allowCompletion();
				} else
					return false;
				particleCount = Math.max(0, particleCount);
				if (particleCount > emitter.getMaxParticleCount()) emitter.setMaxParticleCount(particleCount * 2);
				//emitter.getEmission().setHigh(particleCount / emitter.getLife().getHighMax() * 1000);
				effect.getEmitters().clear();
				effect.getEmitters().add(emitter);
				return false;
			}

			
			public boolean mouseMoved (int x, int y) {
				return false;
			}

			@Override
			public boolean scrolled (int amount) {
				return false;
			}

            @Override
            public boolean touchMoved(int i, int i1) {
                return true;
            }
		};

		Gdx.input.setInputProcessor(inputProcessor);
	}

	@Override
	public void dispose () {
		spriteBatch.dispose();
		effect.dispose();
	}

	public void render () {
		spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		float delta = Gdx.graphics.getDeltaTime();
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		spriteBatch.begin();
		//effect.draw(spriteBatch, delta);
                drawEmitter();
		spriteBatch.end();
		fpsCounter += delta;
		if (fpsCounter > 3) {
			fpsCounter = 0;
			int activeCount = emitters.get(emitterIndex).getActiveCount();
			Gdx.app.log("libgdx", activeCount + "/" + particleCount + " particles, FPS: " + Gdx.graphics.getFramesPerSecond());
		}
	}

        private void drawEmitter(){
            for (ParticleEmitter emitter : effect.getEmitters()) {
               emitter.setPosition(100, 100);
               emitter.duration = 100;
               emitter.getSpawnShape().setShape(ParticleEmitter.SpawnShape.point);
               System.out.println(" emitter.scale " + emitter.getScale().getHighMax());
               System.out.println(" emitter.scale " + emitter.getScale().getHighMin());
               System.out.println(" emitter.scale " + emitter.getScale().getLowMax());
               System.out.println(" emitter.scale " + emitter.getScale().getLowMin());
               System.out.println(" emitter.getScale " + emitter.getScale().getScaling()[0]);
               System.out.println(" emitter.getVelocity() " + emitter.getVelocity().getHighMax());
               System.out.println(" emitter.getVelocity() " + emitter.getVelocity().getHighMin());
               System.out.println(" emitter.getVelocity() " + emitter.getVelocity().getLowMax());
               System.out.println(" emitter.getVelocity() " + emitter.getVelocity().getLowMin());
             //  emitter.getTint().setActive(false);
               System.out.println(fastRandom());
              // emitter.getVelocity().setHighMax(450);
              // emitter.getVelocity().setHighMin(35);
               //emitter.getVelocity().setHighMin(fastRandom() * 19);
               
               //emitter.getVelocity().setHighMin(fastRandom()* 1 - 8);
               
              // emitter.addParticles(2);
              // emitter.getSpawnShape().setEdges(true);
              // emitter.getSpawnShape().setShape(ParticleEmitter.SpawnShape.point);
              // emitter.getDelay().setLowMax(1000.0f);
			//	if (emitter.getSprite() == null && emitter.getImagePath() != null) loadImage(emitter);
				//boolean enabled = isEnabled(emitter);
			//	if (enabled) {
					if (emitter.getSprite() != null) emitter.draw(spriteBatch, Gdx.graphics.getDeltaTime());
					//activeCount += emitter.getActiveCount();
					//if (emitter.isContinuous()) complete = false;
					//if (!emitter.isComplete()) complete = false;
			//	}
			}
        }
	public boolean needsGL20 () {
		return false;
	}

    @Override
    public void resize(int i, int i1) {
       
    }

    @Override
    public void pause() {
        
    }

    @Override
    public void resume() {
        
    }
    private void addBurst(int min, int max){
        int numparticles = fastRandom() * min + (max-min);
        for(int i = min; i<max; i++){
            
        }
    }
    
    private int fastRandom(){
     fastrandomseed ^= (fastrandomseed << 21);
			fastrandomseed ^= (fastrandomseed >>> 35);
			fastrandomseed ^= (fastrandomseed << 4);
			return (fastrandomseed * FASTRANDOMTOFLOAT);
    }
   private int getrandom(){
       return (int)Math.random()*200 + 10;
   }
    
    private ParticleEmitter addParticle(ParticleEmitter emitter,float x, float y, float startScale,float speedx, float speedy ){
         emitter.setPosition(x, y);
         emitter.getScale().setScaling(new float[]{1});
        return emitter;
        
    }
}